package com.rzico.sbl.ui.statistic

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityUnionSendBinding
import com.rzico.sbl.model.UnionSendTotal
import com.rzico.sbl.other.*
import com.rzico.sbl.viewmodel.StatisticUnionViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.dp2px
import com.xinnuo.common.helper.sp2px
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class UnionSendActivity : BaseActivity(R.layout.activity_union_send) {

    private val mBinding by lazy { ActivityUnionSendBinding.bind(getRootView()) }

    private var mDateMode = "4"
    private var mBeginDate = getFirstDate()
    private var mEndDate = getEndDate()

    override fun getViewModel() = get<StatisticUnionViewModel>()

    override fun initData() {
        initTitle(title = "配送结算")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关配送结算信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData() }
                )

                view.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            (mList[pos] as UnionSendTotal).sellerName
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setTextPaddingLeft(dp2px(25f))
                        setHeaderHeight(dp2px(42f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<UnionSendTotal>(R.layout.item_statistic_union_send) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_send_num, bean.subQuantity)
                            text(R.id.item_send_money, bean.subTotal.formatDecimal())
                            text(R.id.item_send_pei, bean.shippingFreight.formatDecimal())
                            text(R.id.item_send_song, bean.adminFreight.formatDecimal())
                            text(R.id.item_send_paper, bean.paper)
                            text(R.id.item_send_cash, bean.cash.formatDecimal())

                            with<LinearLayout>(R.id.item_send_list) { layout ->
                                layout.removeAllViews()

                                bean.products.forEach { item ->
                                    layout.addView(
                                        inflate<View>(R.layout.content_statistic_union_goods).apply {
                                            find<TextView>(R.id.content_name).text = item.name
                                            find<TextView>(R.id.content_num).text = item.quantity
                                            find<TextView>(R.id.content_money).text =
                                                item.cost.formatDecimal()
                                        }
                                    )
                                }
                            }

                            clicked {
                                startActivityEx<UnionSendRecordActivity>(
                                    "sellerId" to bean.sellerId,
                                    "dateMode" to mDateMode,
                                    "beginDate" to mBeginDate,
                                    "endDate" to mEndDate
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    override fun initListener() {
        mBinding
            .sendTime
            .apply {
                getViewModel().mUncheckTimeList[3].isChecked = true
                timeHint.text = getViewModel().mUncheckTimeList[3].hint

                timeLayout.oneClick {
                    getViewModel()
                        .showDropTimeFilter(
                            getIContext(),
                            timeHint,
                            timeHintArrow,
                            timeLayout,
                            getViewModel().mUncheckTimeList
                        ) { bean ->

                            (bean.id != "7")
                                .condition {
                                    getViewModel()
                                        .mUncheckTimeList
                                        .last()
                                        .name = "自定义时间"
                                }

                            when (bean.id) {
                                "1" -> {
                                    mDateMode = "1"
                                    mBeginDate = getTodayDate()
                                    mEndDate = getTodayDate()

                                    updateList()
                                }
                                "2" -> {
                                    mDateMode = "2"
                                    mBeginDate = getBeforeDate()
                                    mEndDate = getBeforeDate()

                                    updateList()
                                }
                                "3" -> {
                                    mDateMode = "3"
                                    mBeginDate = getMondayDate()
                                    mEndDate = getSundayDate()

                                    updateList()
                                }
                                "4" -> {
                                    mDateMode = "4"
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()

                                    updateList()
                                }
                                "5" -> {
                                    mDateMode = "5"
                                    mBeginDate = getBeforeFirstDate()
                                    mEndDate = getBeforeEndDate()

                                    updateList()
                                }
                                "6" -> {
                                    mDateMode = "6"
                                    mBeginDate = getYearFirstDate()
                                    mEndDate = getYearEndDate()

                                    updateList()
                                }
                                "7" -> {
                                    showTimeRangeDialog(
                                        isCurrentDate = true,
                                        isAfterLimited = true,
                                        onResult = { start, end ->
                                            getViewModel()
                                                .mUncheckTimeList
                                                .forEach { it.isChecked = false }

                                            getViewModel()
                                                .mUncheckTimeList
                                                .last()
                                                .isChecked = true

                                            mDateMode = "7"
                                            mBeginDate = start
                                            mEndDate = end
                                            bean.name = "$mBeginDate ~ $mEndDate"
                                            timeHint.text = bean.name

                                            updateList()
                                        }
                                    )
                                }
                            }
                        }
                }
            }
    }

    /**
     * 配送结算列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .sendList(
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.clear()
                    mList.addItems(it.data)

                    mBinding.apply {
                        sendNum.text = it.summary?.subQuantity.orEmpty("0")
                        sendMoney.text = it.summary?.subTotal.formatDecimal()
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData()
    }

}